package sort;

/**
 * @description: 排序父类 抽出一些共用方法继承
 * @title: BaseSort
 * @author: zp
 * @date: 2021/11/14 16:07
 */
public class BaseSort {
    /**
     * @description: 小于的方法
     * @author: zp
     * @date: 2021/11/16 20:51
     * @param: v
     * @param: w
     * @return: boolean
     */
    public static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }

    /**
     * @description: 大于的方法
     * @author: zp
     * @date: 2021/11/16 20:51
     * @param: v
     * @param: w
     * @return: boolean
     */
    public static boolean more(Comparable v, Comparable w) {
        return v.compareTo(w) > 0;
    }

    /**
     * @description: 相等的方法
     * @author: zp
     * @date: 2021/11/16 20:51
     * @param: v
     * @param: w
     * @return: boolean
     */
    public static boolean equal(Comparable v, Comparable w) {
        return v.compareTo(w) == 0;
    }

    /**
     * @description: 判断是否排序正确
     * @author: zp
     * @date: 2021/11/16 20:51
     * @param: a
     * @return: boolean
     */
    public static boolean isSorted(Comparable[] a) {
        for (int i = 1; i < a.length; i++) {
            if (less(a[i], a[i - 1])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @description: 判断一定长度的元素是否排序正确
     * @author: zp
     * @date: 2021/11/16 20:52
     * @param: a
     * @param: lo
     * @param: hi
     * @return: boolean
     */
    public static boolean isSorted(Comparable[] a, int lo, int hi) {
        for (int i = lo + 1; i < hi; i++) {
            if (less(a[i], a[i - 1])) {
                return false;
            }
        }
        return true;
    }

    /**
     * @description: 元素交换
     * @author: zp
     * @date: 2021/11/16 20:52
     * @param: a
     * @param: i
     * @param: j
     * @return: void
     */
    public static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

    /**
     * @description: 显示数组
     * @author: zp
     * @date: 2021/11/16 20:53
     * @param: a
     * @return: void
     */
    public static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
    }
}
